package com.skypan.wanglo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    //定义访问的地址
    private final String URL_STR = "http://122.51.170.127:8088/transportservice/action/GetSUserInfo.do";
    private ListView mListview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取列表控件
        mListview = (ListView) findViewById(R.id.listview);
        //----进行网络请求
        //1.0 创建网络请求队列 ， 生成变量名和变量类型CTRL+ALT+V
        RequestQueue queue = Volley.newRequestQueue(this);
        //2.0创建请求任务
        JSONObject params = new JSONObject();
        //需要填写请求参数,{"UserName":"user1"}
        try {
            params.put("UserName", "user1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
			Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");
		Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");
		Log.e("---","水水水水水水水水水水水水水水水说是发射点发射点士大夫撒旦手动阀手动阀手动阀手动阀水水水水水水水水水水水水水");
		Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");
		Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");
	
		
        JsonObjectRequest request = new JsonObjectRequest(
                Request.Method.POST,//请求方式POST
                URL_STR,//请求地址
                params,//请求的json参数
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        //请求成功的监听
                        //将请求成功的数据，显示出来

                        //1.0 解析数据 , 将json数据装换成javaBean对象
                        User user = new Gson().fromJson(jsonObject.toString(), User.class);
                        //2.0 将数据显示出来
                        showData(user.getROWS_DETAIL());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //请求失败
                Log.e("TAG", "---------------" +
                        volleyError.getMessage());//如果请求失败，将错误信息打印出来
            }
        }
        );
        //3.0将任务添加到队列（也就是发送请求）
        queue.add(request);
		
			Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");
		Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");
		Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");
		Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");
		Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");
		Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");
		Log.e("---","水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水水");


        //列表的点击事件
        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取被点击的数据
                User.ROWSDETAILBean user = (User.ROWSDETAILBean) mListview.getAdapter().getItem(position);
                Toast.makeText(MainActivity.this, user.getPname() + "被点击了", Toast.LENGTH_SHORT).show();
            }
        });

        //长按事件
        mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                //获取被点击的数据
                User.ROWSDETAILBean user = (User.ROWSDETAILBean) mListview.getAdapter().getItem(position);
                Toast.makeText(MainActivity.this, user.getPname() + "被长按了", Toast.LENGTH_SHORT).show();
                return true;//true ,代表事件完全被处理，不再向下传递
            }
        });
    }


    //显示数据的方法
    private void showData(List<User.ROWSDETAILBean> rows_detail) {
        mListview.setAdapter(new MyAdapter(rows_detail));
    }


    /**
     * ListView的适配器，用于显示用户信息
     */
    private class MyAdapter extends BaseAdapter {
        private TextView mTvName;
        private TextView mTvPid;

        //列表数据的集合
        private List<User.ROWSDETAILBean> rows_detail;

        public MyAdapter(List<User.ROWSDETAILBean> rows_detail) {
            this.rows_detail = rows_detail;
        }


        //返回列表数据的数量（必写）
        @Override
        public int getCount() {
            return rows_detail.size();
        }

        //返回对应位置的用户数据，getView中调用
        @Override
        public User.ROWSDETAILBean getItem(int position) {
            return rows_detail.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        //没有采用复用缓存
        @Override
        public View getView(int position/*位置编号*/, View convertView, ViewGroup parent) {
            //需要一个列表布局
            View view = View.inflate(MainActivity.this, R.layout.item, null);
            //显示姓名的控件
            mTvName = (TextView) view.findViewById(R.id.tv_name);
            //显示身份证号的控件
            mTvPid = (TextView) view.findViewById(R.id.tv_pid);

            //获取要显示的数据
            User.ROWSDETAILBean user = getItem(position);
            //设置姓名
            mTvName.setText(user.getPname());
            //设置身份证号
            mTvPid.setText(user.getPcardid());
            return view;
        }
    }
}
